﻿// Decompiled with JetBrains decompiler
// Type: XamlEditor.ImageAdorner
// Assembly: XamlEditor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 2AF0471B-7BFE-4738-893B-A4F91E243353
// Assembly location: C:\Users\KasimGulcan\Desktop\Kosgeb\DynaCAD\Project\lib\XamlEditor.dll

using System.Collections;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace XamlEditor
{
  public class ImageAdorner : Adorner
  {
    private Thumb TopLeft;
    private Thumb BottomRight;
    private VisualCollection VisualChildren;

    protected override int VisualChildrenCount
    {
      get
      {
        return this.VisualChildren.Count;
      }
    }

    public ImageAdorner(Image AdornedElement)
      : base((UIElement) AdornedElement)
    {
      this.VisualChildren = new VisualCollection((Visual) this);
      this.TopLeft = new Thumb();
      this.BottomRight = new Thumb();
      this.BuildAdorners();
      this.ThumbHandler();
    }

    private void BuildAdorners()
    {
      this.TopLeft.Height = this.TopLeft.Width = 10.0;
      this.TopLeft.Opacity = 0.8;
      this.TopLeft.Background = (Brush) new SolidColorBrush(Colors.Black);
      this.TopLeft.Style = (Style) this.FindResource((object) "TopLeft");
      this.VisualChildren.Add((Visual) this.TopLeft);
      this.BottomRight.Height = this.BottomRight.Width = 10.0;
      this.BottomRight.Opacity = 0.8;
      this.BottomRight.Background = (Brush) new SolidColorBrush(Colors.Black);
      this.BottomRight.Style = (Style) this.FindResource((object) "BottomRight");
      this.VisualChildren.Add((Visual) this.BottomRight);
    }

    private void ThumbHandler()
    {
      this.TopLeft.DragDelta += new DragDeltaEventHandler(this.TopLeftDragDelta);
      this.BottomRight.DragDelta += new DragDeltaEventHandler(this.BottomRightDragDelta);
    }

    private void TopLeftDragDelta(object sender, DragDeltaEventArgs e)
    {
      Image image = this.AdornedElement as Image;
      Point position = Mouse.GetPosition((IInputElement) image.Parent);
      Canvas.SetLeft((UIElement) image, position.X);
      Canvas.SetTop((UIElement) image, position.Y);
    }

    private void BottomRightDragDelta(object sender, DragDeltaEventArgs e)
    {
      Image image = this.AdornedElement as Image;
      Point position = Mouse.GetPosition((IInputElement) image.Parent);
      double width = image.Source.Width;
      double height = image.Source.Height;
      ScaleTransform scaleTransform = new ScaleTransform((position.X - Canvas.GetLeft((UIElement) image)) / width, (position.Y - Canvas.GetTop((UIElement) image)) / height);
      image.RenderTransform = (Transform) scaleTransform;
    }

    public override GeneralTransform GetDesiredTransform(GeneralTransform transform)
    {
      Image image = this.AdornedElement as Image;
      if (this.VisualChildren != null)
      {
        foreach (Thumb thumb in Enumerable.OfType<Thumb>((IEnumerable) this.VisualChildren))
        {
          if (image.RenderTransform is ScaleTransform)
            thumb.RenderTransform = (Transform) new ScaleTransform(1.0 / ((ScaleTransform) image.RenderTransform).ScaleX, 1.0 / ((ScaleTransform) image.RenderTransform).ScaleY);
          thumb.RenderTransformOrigin = new Point(0.5, 0.5);
        }
      }
      return base.GetDesiredTransform(transform);
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
      Rect rect = new Rect(this.AdornedElement.DesiredSize);
      Image image = this.AdornedElement as Image;
      SolidColorBrush solidColorBrush = new SolidColorBrush(Colors.Green);
      solidColorBrush.Opacity = 0.3;
      Pen pen = new Pen((Brush) solidColorBrush, 1.5);
      drawingContext.DrawRectangle((Brush) null, pen, new Rect(new Point(0.0, 0.0), new Point(image.Width, image.Height)));
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
      Image image = this.AdornedElement as Image;
      this.TopLeft.Arrange(new Rect(new Point(-20.0, -20.0), new Point(20.0, 20.0)));
      this.BottomRight.Arrange(new Rect(new Point(-20.0, -20.0), new Point(image.Source.Width * 2.0 + 20.0, image.Source.Height * 2.0 + 20.0)));
      return finalSize;
    }

    protected override Visual GetVisualChild(int index)
    {
      return this.VisualChildren[index];
    }
  }
}
